﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YJKRevitStairs.StairsModel.CommonUnit;

namespace YJKRevitStairs.HYElevator.Manage
{
	internal class DeleteElevatorCls
	{
		public DeleteElevatorCls(UIDocument UIDoc)
		{
			this.m_uiDoc = UIDoc;
			this.m_Doc = UIDoc.Document;
			this.m_FloorPlan = Common.GetSortFloorViews(this.m_Doc, double.MinValue, double.MaxValue);
		}

		public bool Run(List<Element> lstSelElem)
		{
			List<ElementId> list = new List<ElementId>();
			foreach (Element element in lstSelElem)
			{
				List<ElementId> list2 = null;
				if (this.GetWholePart(element.Id, out list2))
				{
					foreach (ElementId elementId in list2)
					{
						Element element2 = this.m_Doc.GetElement(elementId);
						List<ElementId> list3 = new List<ElementId>();
						string empty = string.Empty;
						int num = 0;
						double num2 = 0.0;
						if (ElevatorXDataManager.ReadElevatorCommont(element2, ref empty, ref num, ref num2, ref list3))
						{
							foreach (ElementId elementId2 in list3)
							{
								foreach (object obj in (this.m_Doc.GetElement(elementId2) as GroupType).Groups)
								{
									Group group = obj as Group;
									list.AddRange(group.GetMemberIds());
								}
							}
							list.Add(elementId);
						}
					}
				}
			}
			Transaction transaction = new Transaction(this.m_Doc, "删除电梯");
			try
			{
				List<ElementId> list4 = list.Distinct<ElementId>().ToList<ElementId>();
				transaction.Start();
				this.m_Doc.Delete(list4);
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
				return false;
			}
			return true;
		}

		public bool GetWholePart(ElementId Selelem, out List<ElementId> WholePartElem)
		{
			WholePartElem = new List<ElementId>();
			Element element = this.m_Doc.GetElement(Selelem);
			double dBottomElev = 0.0;
			double dTopElev = 0.0;
			bool flag = this.GetStartAndEndLevel(element, ref dBottomElev, ref dTopElev);
			if (!flag)
			{
				return flag;
			}
			WholePartElem = new List<ElementId>();
			Autodesk.Revit.DB.View activeView = this.m_Doc.ActiveView;
			this.m_uiDoc.ActiveView = this.m_TopView;
			flag = this.GetInterSectionElem(element, dBottomElev, dTopElev, ref WholePartElem);
			this.m_uiDoc.ActiveView = activeView;
			return true;
		}

		private void GetStartLayerAndEndLayerName(Element elem, ref string strFirstLevel, ref string strEndLevel)
		{
			string text = string.Empty;
			int num = 0;
			double num2 = 0.0;
			if (elem is Group)
			{
				string name = (elem as Group).GroupType.Name;
				int num3 = name.IndexOf("#");
				if (num3 == -1)
				{
					return;
				}
				int num4 = name.LastIndexOf("#");
				if (num4 == -1 || num3 == num4)
				{
					return;
				}
				text = name.Substring(num3 + 1, num4 - num3 - 1);
			}
			else
			{
				List<ElementId> list = new List<ElementId>();
				ElevatorXDataManager.ReadElevatorCommont(elem, ref text, ref num, ref num2, ref list);
			}
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			int num5 = text.IndexOf('@');
			if (num5 == 0 || num5 == text.Count<char>() - 1)
			{
				return;
			}
			strFirstLevel = text.Substring(0, num5);
			strEndLevel = text.Substring(num5 + 1);
		}

		private bool GetStartAndEndLevel(Element elem, ref double dBottomElev, ref double dTopElev)
		{
			string strBottomLevel = string.Empty;
			string strTopLevel = string.Empty;
			this.GetStartLayerAndEndLayerName(elem, ref strBottomLevel, ref strTopLevel);
			if (string.IsNullOrEmpty(strBottomLevel) || string.IsNullOrEmpty(strTopLevel))
			{
				return false;
			}
			Autodesk.Revit.DB.View view = this.m_FloorPlan.Find((Autodesk.Revit.DB.View e) => e.GenLevel.Name.CompareTo(strBottomLevel) == 0);
			if (view == null)
			{
				return false;
			}
			this.m_TopView = this.m_FloorPlan.Find((Autodesk.Revit.DB.View e) => e.GenLevel.Name.CompareTo(strTopLevel) == 0);
			if (this.m_TopView == null)
			{
				return false;
			}
			dBottomElev = view.GenLevel.Elevation;
			dTopElev = this.m_TopView.GenLevel.Elevation;
			return true;
		}

		private bool GetInterSectionElem(Element elem, double dBottomElev, double dTopElev, ref List<ElementId> lstSecElemId)
		{
			Transaction transaction = new Transaction(this.m_Doc, "求碰撞实体");
			Autodesk.Revit.DB.View activeView = this.m_Doc.ActiveView;
			bool result;
			try
			{
				BoundingBoxXYZ boundingBoxXYZ = elem.get_BoundingBox(this.m_Doc.ActiveView);
				this.m_uiDoc.ActiveView = this.m_TopView;
				transaction.Start();
				FloorType floorType = null;
				if (!this.GetFloorType(dBottomElev, dTopElev, ref floorType))
				{
					transaction.RollBack();
					this.m_uiDoc.ActiveView = activeView;
					result = false;
				}
				else
				{
					XYZ min = boundingBoxXYZ.Min;
					XYZ max = boundingBoxXYZ.Max;
					int num = 0;
					if (elem is Group)
					{
						num = 1;
					}
					List<XYZ> list = new List<XYZ>();
					list.Add(new XYZ(min.X - (double)num, min.Y - (double)num, dTopElev));
					list.Add(new XYZ(min.X - (double)num, max.Y + (double)num, dTopElev));
					list.Add(new XYZ(max.X + (double)num, max.Y + (double)num, dTopElev));
					list.Add(new XYZ(max.X + (double)num, min.Y - (double)num, dTopElev));
					CurveArray curveArray = new CurveArray();
					for (int i = 0; i < list.Count; i++)
					{
						if (i < list.Count - 1)
						{
							curveArray.Append(Line.CreateBound(list[i], list[i + 1]));
						}
						else
						{
							curveArray.Append(Line.CreateBound(list[list.Count - 1], list[0]));
						}
					}
					if (this.m_TopView.SketchPlane == null)
					{
						Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(this.m_TopView.Origin, this.m_TopView.RightDirection, this.m_TopView.UpDirection);
						SketchPlane sketchPlane = SketchPlane.Create(this.m_Doc, plane);
						this.m_TopView.SketchPlane = sketchPlane;
					}
					Floor floor = this.m_Doc.Create.NewFloor(curveArray, floorType, this.m_TopView.GenLevel, false);
					if (floor == null)
					{
						transaction.RollBack();
						this.m_uiDoc.ActiveView = activeView;
						result = false;
					}
					else
					{
						Parameter parameter = floor.GetParameter(BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM);
						if (parameter != null && !parameter.IsReadOnly)
						{
							parameter.Set(2);
						}
						this.m_Doc.Regenerate();
						ElementIntersectsElementFilter elementIntersectsElementFilter = new ElementIntersectsElementFilter(floor);
						FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Doc);
						filteredElementCollector.WherePasses(elementIntersectsElementFilter);
						foreach (Element element in filteredElementCollector)
						{
							lstSecElemId.Add(element.Id);
						}
						transaction.RollBack();
						this.m_uiDoc.ActiveView = activeView;
						result = true;
					}
				}
			}
			catch (Exception)
			{
				if (transaction.IsValidObject && transaction.HasStarted())
				{
					transaction.RollBack();
				}
				this.m_uiDoc.ActiveView = activeView;
				result = false;
			}
			return result;
		}

		private bool GetFloorType(double dBottomElev, double dTopElev, ref FloorType SelFloorType)
		{
			double num = dTopElev - dBottomElev + 2.0;
			string strFloorType = "查找电梯楼板类型";
			ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(FloorType));
			List<Element> list = new FilteredElementCollector(this.m_Doc).WherePasses(elementClassFilter).ToElements() as List<Element>;
			Element element = list.Find((Element e) => e.Name.CompareTo(strFloorType) == 0);
			if (element == null)
			{
				using (List<Element>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Element element2 = enumerator.Current;
						FloorType floorType = element2 as FloorType;
						if (floorType != null && !floorType.IsFoundationSlab)
						{
							SelFloorType = (floorType.Duplicate(strFloorType) as FloorType);
							Parameter parameter = SelFloorType.GetParameter(BuiltInParameter.FLOOR_ATTR_DEFAULT_THICKNESS_PARAM);
							if (parameter != null)
							{
								double num2 = parameter.AsDouble();
								double num3 = num - num2;
								if (Common.IsDoubleLessThanZero(num3))
								{
									return true;
								}
								CompoundStructure compoundStructure = SelFloorType.GetCompoundStructure();
								IList<CompoundStructureLayer> layers = compoundStructure.GetLayers();
								layers.Add(new CompoundStructureLayer
								{
									Width = num3,
                                    Function = (MaterialFunctionAssignment)4
								});
								compoundStructure.SetLayers(layers);
								SelFloorType.SetCompoundStructure(compoundStructure);
								return true;
							}
						}
					}
					return false;
				}
			}
			SelFloorType = (element as FloorType);
			return true;
		}

		private UIDocument m_uiDoc;

		private Document m_Doc;

		private List<Autodesk.Revit.DB.View> m_FloorPlan;

		private Autodesk.Revit.DB.View m_TopView;
	}
}
